package com.zhisheng.core.security.loader;

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.web.context.support.WebApplicationContextUtils;

import com.zhisheng.core.security.support.SecurityManagerSupport;

public class SecurityLoaderListener implements ServletContextListener {

	@Override
	public void contextInitialized(ServletContextEvent servletContextEvent) {
	      ServletContext servletContext = servletContextEvent.getServletContext();   
	      SecurityManagerSupport securityManager = this.getSecurityManager(servletContext);   
	           
	      Map<String, String> urlAuthorities = securityManager.listUrlAuthorities();   
	      servletContext.setAttribute("urlAuthorities", urlAuthorities);   

	}
	
	@Override
	public void contextDestroyed(ServletContextEvent servletContextEvent) {
		 servletContextEvent.getServletContext().removeAttribute("urlAuthorities");   

	}
	
	protected SecurityManagerSupport getSecurityManager(ServletContext servletContext) {   
	       return (SecurityManagerSupport) WebApplicationContextUtils.getWebApplicationContext(servletContext).getBean("securityManager");    
	
	}    

}
